#include <iostream>
using std::cout;
using std::endl;

class Singleton{
public:
    static Singleton* getInstance(){
//        if(_pInstance == nullptr){
//            _pInstance = new Singleton();
//        }
        pthread_once(&_once_control, init);
        return _pInstance;
    }

    void print(){
        cout << "hello" << endl;
    }

private:
    friend class AutoRealease;
    static void init(){
        _pInstance = new Singleton();
    }
    Singleton(){}
    ~Singleton(){}
    static Singleton* _pInstance;
    static pthread_once_t _once_control;
};
Singleton* Singleton::_pInstance = nullptr;
pthread_once_t Singleton::_once_control = PTHREAD_ONCE_INIT;

class AutoRealease{
public:
//    AutoRealease(Singleton *p)
//        :_ps(p)
//    {}

    ~AutoRealease(){
        if(Singleton::_pInstance){
            delete Singleton::_pInstance;
            Singleton::_pInstance = nullptr;
        }
    }

private:
    Singleton *_ps;
};

void test(){
    AutoRealease ar;
    Singleton::getInstance()->print();
    Singleton::getInstance()->print();
}

int main()
{
    test();
    return 0;
}

